package com.xingxing.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
import com.xingxing.Activity.databinding.ActivityLoginBinding;
import com.xingxing.Bean.Login;
import com.xingxing.Util.CommonUtils;
import com.xingxing.Util.APIConfig;
import com.xingxing.Interface.OkSuccessInterface;
import com.xuexiang.xui.utils.StatusBarUtils;
import com.xuexiang.xui.utils.XToastUtils;

import java.util.HashMap;
import java.util.Map;

import static com.xingxing.Util.CommonUtils.*;

public class LoginActivity extends AppCompatActivity {
    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        //设置沉浸式状态栏
        StatusBarUtils.translucent(this);
        //设置黑色字体状态栏
        StatusBarUtils.setStatusBarLightMode(this);
        setContentView(binding.getRoot());
        CommonUtils.init(getApplication());

        /**
         * 是否是从注销登录进来的
         */
        String userAccount = null;
        try {
            if (CommonUtils.GetAppData().equals("false")) {
                userAccount = CommonUtils.GetUserInfo().getUserAccount();
                binding.etUserAccount.setText(userAccount);
                if (userAccount != null) {
                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else if (CommonUtils.GetAppData().equals("true")) {
                userAccount = CommonUtils.GetUserInfo().getUserAccount();
                binding.etUserAccount.setText(userAccount);
                binding.etUserPassword.setText(CommonUtils.GetUserInfo().getUserPassword());
            }
        } catch (Exception e) {
            XToastUtils.toast("您还没有登录！");
        }

        /**
         * 登 录
         */
        binding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (binding.etUserAccount.getText().toString().isEmpty()
                        || binding.etUserPassword.getText().toString().isEmpty()) {
                    XToastUtils.info("账号或密码为空！");
                } else {
                    binding.btnLogin.setText("正在登录...");
                    binding.btnLogin.setEnabled(false);
                    Map<String, Object> map = new HashMap<>();
                    map.put("userAccount", binding.etUserAccount.getText().toString());
                    map.put("userPassword", binding.etUserPassword.getText().toString());
                    OKJsonPost(LoginActivity.this, new Gson().toJson(map), APIConfig.Login, new OkSuccessInterface() {
                        @Override
                        public void OnSuccess(String json) {
                            Login loginBean = new Gson().fromJson(json, Login.class);
                            if (loginBean.getStatus().getStatus().equals("success")) {
                                XToastUtils.success("登录成功！");
                                SaveKey("UserInfo", "info", json);
                                SaveKey("UserInfo", "isFromLogout", "false");
                                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else if (loginBean.getStatus().getStatus().equals("userNoExist")) {
                                XToastUtils.warning("用户不存在！");
                            } else if (loginBean.getStatus().getStatus().equals("userPasswordError")) {
                                XToastUtils.warning("密码错误！");
                            }
                        }
                    });
                    binding.btnLogin.setEnabled(true);
                    binding.btnLogin.setText("登  录");
                }
            }
        });

        /**
         * 注 册
         */
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        /**
         * 忘记密码
         */
        binding.btnForgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                finish();
            }
        });

        /**
         * 验证码登录
         */
        binding.btnLoginByCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, LoginByCodeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}